package demo1;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

/**
 * Created with IntelliJ IDEA.
 * Description: 遍历顺序表
 * User: cjc
 * Date: 2025-04-15
 * Time: 13:33
 */
public class Test {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        System.out.println("--------for循环遍历--------");
        for(int i = 0; i < list.size();i++) {
            System.out.print(list.get(i)+" ");
        }
        System.out.println();
        System.out.println("--------foreach循环遍历--------");
        for (Integer integer : list) {
            System.out.print(integer + " ");
        }
        System.out.println();
        System.out.println("--------迭代器正序输出1--------");
        Iterator<Integer> it = list.listIterator();
        while (it.hasNext()) {
            System.out.print(it.next()+" ");
        }
        System.out.println();
        System.out.println("--------迭代器正序输出2--------");
        ListIterator<Integer> its = list.listIterator();
        while (its.hasNext()) {
            System.out.print(its.next()+" ");
        }
        System.out.println();
        System.out.println("--------迭代器正序输出3（从指定下标位置输出）--------");
        ListIterator<Integer> itss = list.listIterator(2);
        while (itss.hasNext()) {
            System.out.print(itss.next()+" ");
        }
        System.out.println();
        System.out.println("--------迭代器逆序输出--------");
        ListIterator<Integer> reits = list.listIterator(list.size());
        while (reits.hasPrevious()) {
            System.out.print(reits.previous()+" ");
        }
    }
}
